package com.yfp.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yfp.common.core.domain.R;
import com.yfp.org.domain.dto.WorkOrderDTO;
import com.yfp.org.domain.entity.OrderDetail;
import com.yfp.org.domain.entity.OrderList;
import com.yfp.org.domain.entity.WorkOrder;
import com.yfp.org.mapper.OrderDetailMapper;
import com.yfp.org.mapper.OrderListMapper;
import com.yfp.org.mapper.WorkOrderMapper;
import com.yfp.org.service.WorkOrderService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.HashMap;
import java.util.List;

import static com.yfp.common.core.utils.PageUtils.startPage;

/**
 * 工单表(WorkOrder)表服务实现类
 *
 *
 * @since 2024-01-02 13:41:12
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {
    @Resource
    private WorkOrderMapper workOrderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private OrderListMapper orderListMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param 主键
     * @return 实例对象
     */
    @Override
    public HashMap<String, Object> queryById(String id) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(id);
        WorkOrder workOrder = this.workOrderMapper.queryById(id);
        OrderList orderList1 = new OrderList();
        orderList1.setOrderSn(workOrder.getOrderSn());
        orderList1.setId(workOrder.getOrderDetailId());
        OrderList orderList = orderListMapper.queryById(orderList1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("workOrder",workOrder);
        map.put("orderDetails",orderList);
        return map;
    }

    /**
     * 分页查询
     *
     * @param workOrder 筛选条件
     * @return 查询结果
     */
    @Override
    public List<WorkOrder> queryByPage(WorkOrder workOrder) {
//        startPage();
        WorkOrderDTO workOrderDTO = new WorkOrderDTO();
        BeanUtil.copyProperties(workOrder,workOrderDTO);
        List<WorkOrder> workOrders = this.workOrderMapper.queryAllByLimit(workOrderDTO);
//        List<WorkOrder> list = workOrders.stream().map(workOrder1 -> {
//            WorkOrder wOrder = new WorkOrder();
//            BeanUtil.copyProperties(workOrder1, wOrder);
//            OrderList orderList = orderListMapper.queryById(workOrder1.getOrderSn());
//            if (Objects.nonNull(orderList)) wOrder.setOrderList(orderList);
//            return wOrder;
//        }).toList();
        return workOrders;
    }

    /**
     * 新增数据
     *
     * @param workOrder 实例对象
     * @return 实例对象
     */
    @Override
    public WorkOrder insert(WorkOrder workOrder) {
        this.workOrderMapper.insert(workOrder);
        return workOrder;
    }

    /**
     * 修改数据
     *
     * @param workOrder 实例对象
     * @return 实例对象
     */
    @Override
    public R update(WorkOrder workOrder) {
        this.workOrderMapper.update(workOrder);
        return R.ok(this.queryById(workOrder.getOrderSn()));
    }
}
